#include <Elementary.h>
#include "NetworkList.h"
#include "popen_wrapper.h"

/* if someone presses the close button on our window - exit nicely */
void win_del(void *data, Evas_Object *obj, void *event_info)
{
	std::cout << "Exit." << std::endl;
	elm_exit();
}

void power_on_cb(void *data, Evas_Object *obj, void *event_info)
{
	std::cout << "Power on!... if only i knew how!" << std::endl;
	//you never know, could be right...
	evas_object_del((Evas_Object*) data);
}

void not_power_on_cb(void *data, Evas_Object *obj, void *event_info)
{
	elm_exit();
}

void show_power_diag(Evas_Object* parent)
{
int mark = 0;

	Evas_Object* in, * lb, * vb, * hb, * pad, * power_bt, * exit_bt;
	in = elm_win_inwin_add(parent);
	
	vb = elm_box_add(in);
	evas_object_size_hint_weight_set(vb, 1.0, 1.0);
	evas_object_show(vb);
	
	pad = elm_frame_add(in);
	elm_frame_style_set(pad, "pad_medium");
	evas_object_size_hint_weight_set(pad, 0.0, 0.0);
	evas_object_size_hint_align_set(pad, 0.5, -1.0);
	elm_box_pack_end(vb, pad);
	evas_object_show(pad);
	
	lb = elm_label_add(in);
	elm_label_label_set(lb, "The Wi-Fi device is powered off.");
	elm_frame_content_set(pad, lb);
	evas_object_show(lb);
	
	pad = elm_frame_add(in);
	elm_frame_style_set(pad, "pad_small");
	elm_box_pack_end(vb, pad);
	evas_object_show(pad);

	hb = elm_box_add(in);
	elm_box_horizontal_set(hb, 1);
	elm_frame_content_set(pad, hb);
	evas_object_show(hb);

	/* 
	power_bt = elm_button_add(in);
	elm_button_label_set(power_bt, "Power Up Wi-Fi");
	evas_object_size_hint_weight_set(power_bt, 1.0, 1.0);
	evas_object_size_hint_align_set(power_bt, -1.0, -1.0);
	elm_box_pack_end(hb, power_bt);
	evas_object_show(power_bt);
	evas_object_smart_callback_add(power_bt, "clicked", power_on_cb, in);
 	*/
	/* add cancel button to box - fill and expand */
	exit_bt = elm_button_add(in);
	elm_button_label_set(exit_bt, "Quit");
	evas_object_size_hint_weight_set(exit_bt, 1.0, 1.0);
	evas_object_size_hint_align_set(exit_bt, -1.0, -1.0);
	elm_box_pack_end(hb, exit_bt);
	evas_object_show(exit_bt);
	/* and when clicked - run "on_cancel" - pass list as data param */
	evas_object_smart_callback_add(exit_bt, "clicked", not_power_on_cb, in);

	elm_win_inwin_content_set(in, vb);
	evas_object_show(in);
}	

typedef struct { Evas_Object* parent; NetworkList* nl;} check_power_cb_data;

int check_power_cb(void* data)
{
	POpen *po = new POpen("iwconfig eth0");
	std::string ret = po->GetData();
	delete po;	
	po = 0;
	bool enabled = (ret.length() > 0);
	
	if(!enabled)
	{
		show_power_diag(((check_power_cb_data*)data)->parent);
	}
	else
	{
		ecore_timer_add(0.1, scan_cb, (void*) ((check_power_cb_data*) data)->nl);
	}
	
	return ECORE_CALLBACK_CANCEL;
}

int elm_main(int argc, char **argv)
{
	/* new window - do the usual and give it a name, title and delete handler */
	Evas_Object* win = elm_win_add(NULL, "shr-fi", ELM_WIN_BASIC);
	elm_win_title_set(win, "Shr-Fi");
	evas_object_smart_callback_add(win, "delete-request", win_del, NULL);

	NetworkList nl(win);
	
	/* show the window */
	evas_object_show(win);

	
	/*test whether the wifi device is powered on, then scan if it is*/
	check_power_cb_data data;
	data.parent = win;
	data.nl = &nl;
	ecore_timer_add(0.45, check_power_cb, (void*) &data);
		
	/* get going and draw/respond to the user */
	std::cout << "elm_run()" << std::endl;
	elm_run();
	/* standard shutdown */
	elm_shutdown();
	return 0;
}
ELM_MAIN()
